class Game extends egret.DisplayObjectContainer {
	private _gun: egret.Bitmap;
	public _actorsLayer: egret.Sprite;
	public _bulletsLayer: egret.Sprite;
	private _bulletShotVector: egret.Point;
	private _bgm: egret.Sound;
	private _gunSound: egret.Sound;
	private _gameUI: GameUI;

	private _damage = 1;

	public constructor() {
		super();
		var bg = new egret.Bitmap();
		bg.texture = RES.getRes("bg_png");
		this.addChild(bg);

		this._actorsLayer = new egret.Sprite();
		this.addChild(this._actorsLayer);
		this._bulletsLayer = new egret.Sprite();
		this.addChild(this._bulletsLayer);

		this._gun = new egret.Bitmap(RES.getRes("gun_gatling_1_png"));
		this._gun.x = 640 / 2;
		this._gun.y = 960;
		this._gun.anchorOffsetX = 45;
		this._gun.anchorOffsetY = 90;
		this.addChild(this._gun);
		this.touchEnabled = true;
		this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouch_Begin, this);
		this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouch_Move, this);
		this.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouch_End, this);

		this._bgm = RES.getRes("music_gaming_mp3");
		var soundChannel = this._bgm.play(0, 0);
		soundChannel.volume = 0.5;
		this._gunSound = RES.getRes("machine_gun_mp3");


		egret.startTick(this.onUpdate, this);

		this._gameUI = new GameUI();
		this.addChild(this._gameUI);
	}

	private _lastTimestamp: number = 0;
	private _zombieSpawnCd: number = 1000;
	private _zombieBirthTimer: number = 0;
	private _bulletCD: number = 100;
	private _bulletTimer: number = 0;

	private onUpdate(timestamp: number): boolean {

		var span = timestamp - this._lastTimestamp;
		this._lastTimestamp = timestamp;
		this._zombieBirthTimer += span;

		if (this._gameUI != null && this._gameUI.Time <= 0) {
			this._gameUI.Time = 0;
			this._gameUI.GameOver(this);
			return;
		}
		
		this._gameUI.Time -= span;

		if (this._zombieBirthTimer > this._zombieSpawnCd) {
			this._zombieBirthTimer = 0;
			this.addZombie();
		}

		if (this._bulletShotVector != null) {
			this._bulletTimer += span;
			if (this._bulletTimer > this._bulletCD) {
				this._bulletTimer = 0;
				this.addBullet();
			}
		}

		for (var i = this._actorsLayer.numChildren - 1; i >= 0; i--) {
			var actor = <Zombie>this._actorsLayer.getChildAt(i);
			actor.onUpdate(span);
			actor._gameUI = this._gameUI;

		}
		for (var i = this._bulletsLayer.numChildren - 1; i >= 0; i--) {
			var bullet = <Bullet>this._bulletsLayer.getChildAt(i);
			bullet.onUpdate(span);
			var point = new egret.Point(bullet.x, bullet.y);
			for (var j = this._actorsLayer.numChildren - 1; j >= 0; j--) {
				var actor = <Zombie>this._actorsLayer.getChildAt(j);
				if (actor.zombieState == ZombieState.Dead) {
					continue;
				}
				if (actor.getColBlock().containsPoint(point)) {

					actor.Hit(this._damage);
					bullet.Destroy();
					break;
				}
			}
		}
		return false;
	}

	private onTouch_Begin(e: egret.TouchEvent) {
		var vx = e.stageX - this._gun.x;
		var vy = e.stageY - this._gun.y;
		this._gun.rotation = Math.atan2(vy, vx) * 180 / Math.PI + 90;
		this._bulletShotVector = new egret.Point(vx, vy);
		this._bulletShotVector.normalize(1);
	}

	private onTouch_Move(e: egret.TouchEvent) {
		var vx = e.stageX - this._gun.x;
		var vy = e.stageY - this._gun.y;
		this._gun.rotation = Math.atan2(vy, vx) * 180 / Math.PI + 90;
		this._bulletShotVector = new egret.Point(vx, vy);
		this._bulletShotVector = new egret.Point(vx, vy);
		this._bulletShotVector.normalize(1);
	}

	private onTouch_End(e: egret.TouchEvent) {
		this._bulletShotVector = null;
		this._zombieBirthTimer = 0;
		this._bulletTimer = 0;
	}

	private addZombie() {
		var zombie: Zombie;
		if (Math.random() <= 0.3) {
			zombie = new Zombie1();
		} else {
			zombie = new Zombie();
		}
		zombie.x = Math.random() * (640 - 40);
		zombie.y = -50;
		this._actorsLayer.addChild(zombie);
	}

	private addBullet() {
		var bullet = new Bullet();
		bullet._vector = this._bulletShotVector;
		bullet.x = this._gun.x - 10;
		bullet.y = this._gun.y;
		bullet.rotation = Math.atan2(this._bulletShotVector.y, this._bulletShotVector.x) * 180 / Math.PI + 90;
		this._bulletsLayer.addChild(bullet);
		this._gunSound.play(0, 1).volume = 0.3;
	}
}